/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import model.BusinessRegisterModel;
import view.BusinessRegisterView;

/**
 *
 * @author Zarc
 */
public class BusinessRegisterController implements IBusinessRegisterController {
    
    private BusinessRegisterView theView;
    private BusinessRegisterModel theModel;
    
    public BusinessRegisterController(BusinessRegisterView theView,BusinessRegisterModel theModel)
    {
        this.theModel = theModel;
        this.theView = theView;
    }
    public BusinessRegisterController(BusinessRegisterView theView)
    {
        this.theView = theView;
    }
    public BusinessRegisterController()
    {
        
    }
    
    @Override
    public boolean registerBusiness(String BusinessName,String BossName,String BossSurname,String Email,
            String AFM,String BossPhone) 
    {
        boolean[] Check = new boolean[6];
        int i,proceed = 0;
            
            Check[0] = checkIfEmpty(BusinessName);
            Check[1] = checkIfEmpty(BossName);
            Check[2] = checkIfEmpty(BossSurname);
            Check[3] = checkIfEmpty(Email);
            Check[4] = checkIfEmpty(AFM);
            
            for(i=0;i<5;i++)
            {
                if(!Check[i])
                {
                    theView.displayErrorMessage("Please Fill All The Nessessary Fields!");
                    break;
                }
                else
                    proceed++;
            }
            
            if(proceed == 5)
            {
                Check[0] = checkInputChars(BusinessName);
                Check[1] = checkInputChars(BossName);
                Check[2] = checkInputChars(BossSurname);
                Check[3] = checkInputEmail(Email);
                Check[4] = checkInputNumbers(AFM);
                Check[5] = checkInputNumbers(BossPhone);
            }
            
            while(proceed == 5)
            {
                for(i=0;i<6;i++)
                    if(!Check[i])
                    {
                        switch(i)
                        {
                            case 0:
                                theView.displayErrorMessage("Please Enter Business Name Again.");
                                theView.setBusinessName(null);
                                break;
                            case 1:
                                theView.displayErrorMessage("Please Enter Your Name Again.");
                                theView.setBossName(null);
                                break;
                            case 2:
                                theView.displayErrorMessage("Please Enter Your Surame Again.");
                                theView.setBossSurname(null);
                                break;
                            case 3:
                                theView.displayErrorMessage("Please Enter Your Email Address Again.");
                                theView.setEmail(null);
                                break;
                            case 4:
                                theView.displayErrorMessage("Please Enter Business AFM Again.");
                                theView.setAFM(null);
                                break;
                            case 5:
                                theView.displayErrorMessage("Please Enter A Valid Phone Number Or Leave Empty.");
                                theView.setBossPhone(null);
                                break;
                            default:                                
                        }
                        //theView.displayErrorMessage("You have had some Erros With The Names!");
                        //break;
                        break;
                    }
                else
                    proceed++;
                break;
            }
        
        if(proceed == 11)
            return true;
        else
            return false;
            
    }
    
    // Checks if value is empty
    @Override
    public boolean checkIfEmpty(String Value)
    {   
        Value = Value.trim();
        if(Value.isEmpty())
            return false;
        else
            return true;
    }
    // Checks if input is a string of numbers
    @Override
    public boolean checkInputNumbers(String Value)
    {
        Value = Value.trim();
        
        if(Value.isEmpty())
            return true;
       
        if(Value.matches("\\d*"))//(pattern.matcher(Value).matches())
            return true;
        else
            return false;
       
    }
    // Checks if input is a string of chars
    @Override
    public boolean checkInputChars(String Value)
    {
        
        if(Value.matches("[a-zA-Z]*"))
            return true;
        else
            return false;
        
    }
    // Checks if input is an email address
    @Override
    public boolean checkInputEmail(String Value)
    {
        if(!Value.contains("@") || !Value.contains("."))
            return false;
        else
            if(Value.indexOf("@") == Value.lastIndexOf("@") && Value.indexOf(".") == Value.lastIndexOf("."))
                return true;
            else
                return false;
    }
    
}
